<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:c="http://java.sun.com/jsp/jstl/core"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.prime.com.tr/ui"
                template="./../template/WorkspaceTemplate.xhtml">

    <ui:define name="top">

    </ui:define>

    <ui:define name="left">

    </ui:define>

    <ui:define name="right">

    </ui:define>

    <ui:define name="content">

        <style>
            .rowHighlight{
                color: red;
                font-weight: bold;
            }
        </style>

        <h:form id="RawMaterialInventoryRecordForm" style="font-size: 85%">
            <h2>View Raw Material Inventory Record</h2>
            <br/>
            <p:panel>
                <p:messages  />
                <br/>
                <h:outputText value="List of Raw Material Inventory Records" style="font-weight: bold; font-size: 110%"/>
                <p:separator/>
                <br/>
                <p:dataTable var="rec" value="#{rmiManagerBean.allRawMaterialInventoryRecord}" emptyMessage="No Raw Material inventory records found." rowStyleClass="#{rec.balance le rec.optimalStock?'rowHighlight':null}" paginator="true" rows="5">  

                    <f:facet name="header">
                        <h:outputText value="Raw Material Inventory Records" />
                    </f:facet>

                    <p:column>  
                        <f:facet name="header">  
                            <h:outputText value="Raw Material" />  
                        </f:facet>  
                        <h:outputText value="#{rec.rawMaterial.name}" />  
                    </p:column>

                    <p:column>  
                        <f:facet name="header">  
                            <h:outputText value="Optimal Stock Level" />  
                        </f:facet>  
                        <h:outputText value="#{rec.optimalStock}">
                            <f:convertNumber pattern="#0.00"/>
                        </h:outputText>
                    </p:column>

                    <p:column>  
                        <f:facet name="header">  
                            <h:outputText value="Total (kg)" />  
                        </f:facet>  
                        <h:outputText value="#{rec.balance*10}">
                            <f:convertNumber pattern="#0.00"/>
                        </h:outputText>
                    </p:column>

                    <p:column>  
                        <f:facet name="header">  
                            <h:outputText value="Options" />  
                        </f:facet>
                        <p:commandButton image="ui-icon ui-icon-pencil" title="Update Optimal Level" actionListener="#{rmiManagerBean.updateRawMaterialOptimalLevel}" update="rmiUpdateForm" oncomplete="rmiDialog.show()">
                            <f:attribute name="selected" value="#{rec}"/>
                        </p:commandButton>
                        <p:commandButton image="ui-icon ui-icon-search" title="Select Product" actionListener="#{rmiManagerBean.viewRawMaterialInventoryRecord}" update="RawMaterialInventoryRecordForm">
                            <f:attribute name="selected" value="#{rec}"/>
                        </p:commandButton>
                    </p:column>

                </p:dataTable>
                <br/>
                <br/>
                <br/>
                <h:panelGroup id="rawMaterialBatches" rendered="#{rmiManagerBean.rmir != null}">

                    <h:outputText value="#{rmiManagerBean.rmir.rawMaterial.name} Inventory" style="font-weight: bold; font-size: 110%"/>
                    <p:separator/>
                    <br/>


                    <p:dataTable var="rec" value="#{rmiManagerBean.rmir.batches}" paginator="true" rows="10">
                        <f:facet name="header">
                            <h:outputText value="#{rmiManagerBean.rmir.rawMaterial.name} Batch Information" />
                        </f:facet>

                        <p:column headerText="Batch No.">
                            <h:outputText value="#{rec.batchNo}"/>
                        </p:column>

                        <p:column headerText="Receipt Date">
                            <h:outputText value="#{rec.dateOrdered}">
                                <f:convertDateTime pattern="MMM dd, yyyy"/>
                            </h:outputText>
                        </p:column>

                        <p:column headerText="Expiry Date">
                            <h:outputText value="#{rec.dateExpired}">
                                <f:convertDateTime pattern="MMM dd, yyyy"/>
                            </h:outputText>
                        </p:column>

                        <p:column headerText="Total Quantity (kg)">
                            <h:outputText value="#{rec.quantity * 10}">
                                <f:convertNumber pattern="#0.00"/>
                            </h:outputText>
                        </p:column>

                        <p:column headerText="Balance (kg)">
                            <h:outputText value="#{rec.quantityAvailable * 10}">
                                <f:convertNumber pattern="#0.00"/>
                            </h:outputText>
                        </p:column>

                        <p:column headerText="Bin Location">
                            <p:commandButton image="ui-icon ui-icon-home" title="Bin Location" actionListener="#{rmiManagerBean.viewBatchLocation}" update="RawMaterialInventoryRecordForm">
                                <f:attribute name="selected" value="#{rec}"/>
                            </p:commandButton>
                        </p:column>

                    </p:dataTable>
                </h:panelGroup>
                <br/>
                <br/>
                <br/>

                <h:panelGroup id="batchLocation" rendered="#{rmiManagerBean.rmib != null}">

                    <h:outputText value="#{rmiManagerBean.rmir.rawMaterial.name} Batch #{rmiManagerBean.rmib.batchNo} Location" style="font-weight: bold; font-size: 110%"/>
                    <p:separator/>
                    <br/>

                    <p:dataTable var="rec" value="#{rmiManagerBean.rmib.locations}">
                        <f:facet name="header">
                            <h:outputText value="Warehouse Locations" />
                        </f:facet>

                        <p:column headerText="Location">
                            <h:outputText value="#{rec.binLocation}"/>
                        </p:column>

                        <p:column headerText="Bin Capacity (kg)">
                            <h:outputText value="#{rec.binCapacity * 10}">
                                <f:convertNumber pattern="#0.00"/>
                            </h:outputText>
                        </p:column>

                        <p:column headerText="Stored Quantity (kg)">
                            <h:outputText value="#{rec.storedQty * 10}">
                                <f:convertNumber pattern="#0.00"/>
                            </h:outputText>
                        </p:column>
                    </p:dataTable>
                </h:panelGroup>
                <br/>
                <br/>
                <br/>
            </p:panel>
            <br/>
            <br/>
            <br/>
        </h:form>

        <p:dialog header="Update Optimal Stock Level" width="600" id="rmiDialog" widgetVar="rmiDialog" modal="true" showEffect="fade" hideEffect="fade" resizable="false" closable="false">
            <h:form id="rmiUpdateForm" style="font-size: 85%">
                <br/>
                <p:messages  />
                <h:panelGrid columns="2" cellpadding="10">
                    <h:outputLabel value="Product Inventory: " style="font-weight: bold"/>
                    <h:outputText value="#{rmiManagerBean.rmir.rawMaterial.name}"/>
                    <h:outputLabel value="Optimal Stock Level: " style="font-weight: bold"/>
                    <h:inputText value="#{rmiManagerBean.rmir.optimalStock}" required="true" requiredMessage="Optimal cannot be empty"/>
                </h:panelGrid>
                <br/>
                <br/>
                <div align="right">
                    <p:commandButton value="Cancel" image="ui-icon ui-icon-close" oncomplete="rmiDialog.hide()"/>
                    <p:commandButton value="Update" image="ui-icon ui-icon-disk" oncomplete="handleUpdateRMIR(xhr, status, args)" action="#{rmiManagerBean.saveRawMaterialOptimalLevel()}" update="rmiUpdateForm RawMaterialInventoryRecordForm"/>
                </div>
            </h:form>
        </p:dialog>

        <script type="text/javascript">  
            function handleUpdateRMIR(xhr, status, args) {  
                if(!args.valid) {  
                    jQuery('#rmiDialog').parent().effect("shake", { times:3 }, 100);
                } else {  
                    rmiDialog.hide();
                }  
            }  
        </script>

    </ui:define>

    <ui:define name="bottom">

    </ui:define>

</ui:composition>
